<?php

class ResultController extends BaseController {

	public function result()
	{
		$game = Game::where('activo', '=', true)->first();
		$teams = Team::where('gameid', '=', $game->id)->get();


		return View::make('results')->with(compact('teams'));
	}

	public function ranking()
	{
		$result = 'ko';
		$teams = Input::get('teams');

		for ($i=0; $i < count($teams); $i++)
		{ 
			DB::table('teams')->where('gameid', '=', $teams[$i]['gameid'])->
								where('teamid', '=', $teams[$i]['teamid'])->
								update(array('ouro' => $teams[$i]['ouro']));
			DB::table('teams')->where('gameid', '=', $teams[$i]['gameid'])->
								where('teamid', '=', $teams[$i]['teamid'])->
								update(array('abastecimentos' => $teams[$i]['abastecimentos']));
			DB::table('teams')->where('gameid', '=', $teams[$i]['gameid'])->
								where('teamid', '=', $teams[$i]['teamid'])->
								update(array('combustiveis' => $teams[$i]['combustiveis']));
			DB::table('teams')->where('gameid', '=', $teams[$i]['gameid'])->
								where('teamid', '=', $teams[$i]['teamid'])->
								update(array('pneus' => $teams[$i]['pneus']));
			DB::table('teams')->where('gameid', '=', $teams[$i]['gameid'])->
								where('teamid', '=', $teams[$i]['teamid'])->
								update(array('tendas' => $teams[$i]['tendas']));
			DB::table('teams')->where('gameid', '=', $teams[$i]['gameid'])->
								where('teamid', '=', $teams[$i]['teamid'])->
								update(array('dinheiro' => $teams[$i]['dinheiro']));

			$teams[$i]['pontuacao'] = 	( $teams[$i]['abastecimentos'] * 30 ) + 
										( $teams[$i]['combustiveis'] * 10 ) + 
										( $teams[$i]['pneus'] * 30 ) +
										( $teams[$i]['tendas'] * 10 ) +
										  $teams[$i]['dinheiro'];

			$result = 'ok';
		}

		return Response::json( array('result' => $result, 'teams' => $teams));
	}

}